/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.dao;

import han.bifang.common.entity.SoaStatDetailLog;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.persist.dao.DaoManager;
import han.bifang.common.persist.page.Page;
import han.bifang.common.persist.page.PagedQuery;
import han.bifang.common.persist.tx.Tx;
import han.bifang.common.persist.tx.TxManager;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Author Kent
 */
public class StatDetailLogDao {

	public static void createStatLog(SoaStatDetailLog s) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			DaoManager.getDao().save(s);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(StatServiceDao.class);
			logger.error("StatDetailLogDao.createStatLog()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createStatLog() error", e);
		} finally {
			tx.end();
		}
	}
	
	public static SoaStatDetailLog getStatLogByMessageLog(String id) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaStatDetailLog l where l.logId = ?";
			
				
			List<SoaStatDetailLog> log = DaoManager.getDao().find(hql,id);
			
			tx.commit();
			if(log.size() > 0){
				return log.get(0);
			}
			return null;
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(StatServiceDao.class);
			logger.error("StatDetailLogDao.getStatLog()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getStatLog() error", e);
		} finally {
			tx.end();
		}
	}

	public static ArrayList searchStatLog(String hql, int page, int pageSize)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			PagedQuery pq = new PagedQuery(hql, page, pageSize);
			Page p = pq.getPage();
			int total = p.getPaginator().getPageNumber();
			ArrayList ret = new ArrayList();
			ret.add(total);
			ret.add(p.getList());
			tx.commit();
			return ret;
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(StatServiceDao.class);
			logger.error("StatDetailLogDao.searchStatLog()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"searchStatLog() error", e);
		} finally {
			tx.end();
		}
	}

}
